/*
 * r_exit.c
 *
 *  Created on: Sep 9, 2013
 *      Authors: Joseph DeLong, Justin Hamric, Alex Newcomer, Alex Bouvy
 */
#include "String.h"

int r_exit(int *exit){

	char question[19] = "Really exit? (y/n)\n";
	char input_buffer[256];
	char original_input[256];
	char* input[6];
	int qlength = 19;
	int bufferSize = 256;
	int i;
	int command_args = 0;

	sys_req(2,1, question, &qlength);
	sys_req(1, 1, input_buffer, &bufferSize);
	
	strcpy(original_input,input_buffer);
	strxfrm(original_input,original_input, strlen(original_input));
	input[0] = strtok(input_buffer," ");
	for(i=1;i<6;i++){
		input[i]=strtok(NULL," ");
		if(input[i]==NULL) break;
		else command_args = i;
	}

	if(strlen(input[0])>1){
		strxfrm(input[0],input[0], strlen(input[0]));
	}
	strcat(input[0],"\0");

	//hacky fix for other tokenization issue
	if(command_args==1){
		if(!(strcmp(input[1],"\n"))){
	        	command_args=0;
	        	input[1] = NULL;
	        }
	}
	
	if(command_args>0){
		printf("'%s' is not valid.\n",original_input);
	}
	else if(!(strcmp(input[0],"Y\0"))) {
		*exit = 0;
	}
	else if(!(strcmp(input[0],"y\0"))) {
		*exit = 0;
	}
	else if(!(strcmp(input[0],"N\0"))) {
		//do nothing
	}
	else if(!(strcmp(input[0],"n\0"))) {
		//do nothing
	}
	else printf("'%s' is not valid.\n",input[0]);
	return 0;
}